const fs = require('fs');
const path = require('path');
const sharp = require('sharp');

const types = ['INTJ','INTP','ENTJ','ENTP','INFJ','INFP','ENFJ','ENFP','ISTJ','ISFJ','ESTJ','ESFJ','ISTP','ISFP','ESTP','ESFP'];
const outDir = path.join(__dirname, '..', 'MyPro', 'static', 'icons', 'mbti');
const size = 256;
const brand = { primary: '#6B5B95' }; // 二色：主色背景 + 白色物品

function itemFor(type){
  switch(type){
    // NT
    case 'INTJ': // 棋马
      return `
        <g>
          <path d="M92 186 h72 v12 h-84 v-12 z" fill="#FFFFFF"/>
          <path d="M156 174 c-4 -24 -16 -38 -36 -46 c12 -10 18 -20 18 -34 c-18 6 -30 12 -44 30 c-6 8 -10 18 -12 30 c-10 6 -16 14 -16 20 v12 h90 z" fill="#FFFFFF"/>
          <circle cx="134" cy="104" r="6" fill="${brand.primary}"/>
        </g>
      `;
    case 'INTP': // 锥形烧瓶
      return `
        <g>
          <rect x="120" y="78" width="16" height="16" rx="3" fill="#FFFFFF"/>
          <path d="M116 94 h24 l26 68 h-76 z" fill="#FFFFFF"/>
          <path d="M124 134 c8 -4 18 -4 28 0" stroke="${brand.primary}" stroke-width="4"/>
        </g>
      `;
    case 'ENTJ': // 王冠
      return `
        <g>
          <path d="M84 170 h88 v18 h-88 z" fill="#FFFFFF"/>
          <path d="M92 170 l16 -28 l20 20 l20 -20 l16 28 z" fill="#FFFFFF"/>
          <circle cx="108" cy="138" r="5" fill="#FFFFFF"/>
          <circle cx="148" cy="138" r="5" fill="#FFFFFF"/>
        </g>
      `;
    case 'ENTP': // 灯泡
      return `
        <g>
          <path d="M128 84 a28 28 0 0 1 28 28 c0 12 -8 22 -16 28 c-6 4 -8 8 -8 12 h-8 c0 -4 -2 -8 -8 -12 c-8 -6 -16 -16 -16 -28 a28 28 0 0 1 28 -28 z" fill="#FFFFFF"/>
          <rect x="116" y="156" width="24" height="14" rx="4" fill="#FFFFFF"/>
        </g>
      `;

    // NF
    case 'INFJ': // 提灯（心）
      return `
        <g>
          <rect x="120" y="84" width="16" height="12" rx="3" fill="#FFFFFF"/>
          <rect x="104" y="96" width="48" height="64" rx="8" fill="#FFFFFF"/>
          <path d="M128 132 c-8 -6 -14 -10 -14 -18 c0 -6 4 -10 10 -10 c4 0 8 3 10 7 c2 -4 6 -7 10 -7 c6 0 10 4 10 10 c0 8 -6 12 -14 18 z" fill="${brand.primary}"/>
        </g>
      `;
    case 'INFP': // 羽毛笔
      return `
        <g>
          <path d="M92 178 l72 -72 c14 -14 28 -14 36 -6 c8 8 8 22 -6 36 l-72 72 l-30 -30 z" fill="#FFFFFF"/>
          <path d="M148 130 c8 -2 14 2 16 8" stroke="${brand.primary}" stroke-width="4"/>
        </g>
      `;
    case 'ENFJ': // 扩音器
      return `
        <g>
          <path d="M96 154 l48 -14 v40 l-48 -14 z" fill="#FFFFFF"/>
          <rect x="86" y="164" width="14" height="14" rx="3" fill="#FFFFFF"/>
        </g>
      `;
    case 'ENFP': // 星火
      return `
        <g>
          <path d="M128 92 l12 24 l26 4 l-20 14 l6 24 l-24 -14 l-24 14 l6 -24 l-20 -14 l26 -4 z" fill="#FFFFFF"/>
        </g>
      `;

    // SJ
    case 'ISTJ': // 盾牌
      return `
        <g>
          <path d="M128 106 L156 116 V138 C156 152 144 162 128 172 C112 162 100 152 100 138 V116 Z" fill="#FFFFFF"/>
          <path d="M128 118 v40" stroke="${brand.primary}" stroke-width="6"/>
        </g>
      `;
    case 'ISFJ': // 医药箱
      return `
        <g>
          <rect x="92" y="128" width="72" height="46" rx="8" fill="#FFFFFF"/>
          <rect x="112" y="118" width="32" height="12" rx="6" fill="#FFFFFF"/>
          <path d="M128 138 v26 M116 151 h24" stroke="${brand.primary}" stroke-width="6"/>
        </g>
      `;
    case 'ESTJ': // 执行清单
      return `
        <g>
          <rect x="96" y="112" width="64" height="72" rx="10" fill="#FFFFFF"/>
          <path d="M106 132 h40 M106 146 h40 M106 160 h40" stroke="${brand.primary}" stroke-width="6"/>
          <path d="M100 124 h56" stroke="#FFFFFF" stroke-width="6"/>
        </g>
      `;
    case 'ESFJ': // 握手
      return `
        <g>
          <path d="M100 154 l18 12 c10 6 18 4 26 -4 l12 -10 l18 12" fill="none" stroke="#FFFFFF" stroke-width="10" stroke-linecap="round"/>
        </g>
      `;

    // SP
    case 'ISTP': // 扳手
      return `
        <g>
          <path d="M104 176 l40 -40 c10 -10 22 -10 30 0 c10 10 10 20 0 30 l-40 40 l-30 -30 z" fill="#FFFFFF"/>
        </g>
      `;
    case 'ISFP': // 画笔
      return `
        <g>
          <path d="M156 140 c12 -2 18 6 16 14 c-8 0 -16 0 -24 8 c-10 10 -18 10 -30 -2 c12 0 20 -6 28 -12 c4 -4 6 -6 10 -8 z" fill="#FFFFFF"/>
          <path d="M108 178 c0 10 10 18 22 14" stroke="${brand.primary}" stroke-width="6"/>
        </g>
      `;
    case 'ESTP': // 奖杯
      return `
        <g>
          <path d="M108 148 h40 c0 18 -12 26 -18 30 h-4 c-6 -4 -18 -12 -18 -30 z" fill="#FFFFFF"/>
          <rect x="120" y="180" width="16" height="10" fill="#FFFFFF"/>
          <rect x="104" y="190" width="48" height="10" fill="#FFFFFF"/>
          <path d="M108 148 c-16 0 -22 -10 -22 -22 h18" stroke="#FFFFFF" stroke-width="8" fill="none"/>
          <path d="M148 148 c16 0 22 -10 22 -22 h-18" stroke="#FFFFFF" stroke-width="8" fill="none"/>
        </g>
      `;
    case 'ESFP': // 麦克风
      return `
        <g>
          <circle cx="148" cy="120" r="16" fill="#FFFFFF"/>
          <rect x="118" y="136" width="20" height="44" rx="8" fill="#FFFFFF"/>
          <path d="M118 168 c-10 10 -22 10 -30 0" stroke="${brand.primary}" stroke-width="6"/>
        </g>
      `;
    default:
      return '';
  }
}

function svgFor(type){
  const svg = `
  <svg width="${size}" height="${size}" viewBox="0 0 ${size} ${size}" xmlns="http://www.w3.org/2000/svg">
    <rect width="100%" height="100%" rx="32" ry="32" fill="${brand.primary}"/>
    <g transform="translate(128 128) scale(1.36) translate(-128 -128)">
      ${itemFor(type)}
    </g>
    <text x="50%" y="92%" fill="#FFFFFF" font-family="Arial, Helvetica, sans-serif" font-weight="700" font-size="30" text-anchor="middle">${type}</text>
  </svg>`;
  return Buffer.from(svg);
}

(async () => {
  if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
  for (const t of types) {
    const svg = svgFor(t);
    const file = path.join(outDir, `${t.toLowerCase()}.png`);
    await sharp(svg).png({ quality: 92 }).toFile(file);
    console.log('Generated', file);
  }
})(); 